<?php

namespace app\common\model;

use think\Model;
use app\common\traits\model\TraitModel;

class UserMessages extends Model
{
	use TraitModel;

    protected $table = 'user_messages';

	const READ_STATUS = 1;
	const NOT_READ_STATUS = 0;

    public function messages()
    {
    	return $this->belongsTo('Messages')->setEagerlyType(0);
    }
    public function getWhere($wheres = [])
    {
        if (is_array($wheres)) {
            foreach ($wheres as $key => $where) {
                if ($where === null || $where === false ) {
                    unset($wheres[$key]);
                }
            }
            return $wheres;
        }
    }
    /**
     * 获得通知条数
     * @param  [type] $status [description]
     * @return [type]         [description]
     */
    public function getNotifyCount($status = self::NOT_READ_STATUS, $user_id = false)
    {
        return self::where($this->getWhere(['type' => 'notify', 'status' => $status, 'users_id' => $user_id]))->count();
    }
    /**
	 * 获得信息条数
	 * @param  integer $status [description]
	 * @return [type]          [description]
	 */
    public function msgNum($status = self::NOT_READ_STATUS, $user_id = false)
    {
    	return self::with('messages')->where($this->getWhere(['type' => 'personal', 'status' => $status, 'users_id' => $user_id]))->count();
    }
    /**
     * 获取通知信息列表
     * @param  [type] $status [description]
     * @return [type]         [description]
     */
    public function getNotifysList($status = self::NOT_READ_STATUS, $user_id = false)
    {
        return self::where($this->getWhere(['type' => 'notify', 'status' => $status, 'users_id' => $user_id]))->select();
    }
    /**
     * 获取信息列表
     * @param  integer $status [description]
     * @return [type]          [description]
     */
    public function getMsgList($user_id = false, $order = 'send_time desc')
    {
    	return self::with('messages')->where(['type' => 'personal', 'users_id' => $user_id])->order($order)->paginate();
    }
    /**
     * 获取信息 读取
     * @param  [type] $msg_id [description]
     * @return [type]         [description]
     */
    public function getMsg($msg_id, $user_id = false)
    {
    	$msg = self::get($this->getWhere(['messages_id' => $msg_id, 'users_id' => $user_id]));
    	$this->setMsgStatus($msg, self::READ_STATUS, $user_id);
	    return $msg;
    }
    /**
     * 删除信息
     * @param  [type] $msg_id [description]
     * @return [type]         [description]
     */
    public function delMsg($msg_id, $user_id = false)
    {
        $where = is_array($msg_id) ? ['messages_id' => ['in', $msg_id]] : ['messages_id' => $msg_id];
        if (false !== $user_id) {
            $where = array_merge($where, ['users_id' => $user_id]);
        }
    	return self::destroy($where);
    }
    /**
     * 设置消息状态
     * @param [type] $msgs   [description]
     * @param [type] $status [description]
     */
    public function setMsgStatus($msgs, $status = self::READ_STATUS, $user_id = false)
    {
    	if (is_array($msgs)) {
            $id = $msgs;
        } elseif (is_numeric($msgs) || is_string($msgs)) {
            $id = $msgs;
        } elseif ($msgs instanceof Model) {
        	$msgs->status = $status;
        	$msgs->save();	
        }
        if (isset($id)) {
            $where = is_array($id) ? ['messages_id' => ['in', $id]] : ['messages_id' => $id];
            if (false !== $user_id) {
                $where = array_merge($where, ['users_id' => $user_id]);
            }
            self::update(['status' => $status], $where);
        } 
        return true;
    }
}
